package com.han.bom.tests;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.han.bom.Answer;
import com.han.bom.ExamResponse;
import com.han.bom.MultipleChoice;
import com.han.bom.RealExam;
import com.han.bom.TrueOrFalse;

public class ExamResponseTest {
	
	private ExamResponse examresponse = null;
	private int correct=0;
	private ArrayList<Answer> answers=null;
	private ArrayList<String> choices =null;
	
	@Before
	public void setUp() throws Exception {
		
		answers = new ArrayList<Answer>();
		
		choices = new ArrayList<String>();
		
		for(int i=0;i<5;i++){
			if(i==0){
				choices.add("cor");
			}
			else{
				choices.add("choice#"+i);
			}
		}
		
		for(int i=0;i<100;i++){
			
			if(i%2==0){

				TrueOrFalse tf=null;
				
				if(i%4==0){
					tf = new TrueOrFalse("TF Question "+(i), "True");
				}
				else{
					tf = new TrueOrFalse("TF Question "+(i), "False");
				}
				
				Answer answer = null;
				
				if(Math.random()>=0.5){
					//correct
					answer = new Answer(tf, tf.getAnswer());
					correct++;
				}
				else{
					//wrong
					answer = new Answer(tf, "wrong answer");
				}
				
				answers.add(answer);
				
			}
			else{
				
				MultipleChoice mc = new MultipleChoice("MC Question "+(i), choices.get(0), choices);
				
				Answer answer = null;
				
				if(Math.random()>=(1.0/choices.size())){
					//correct
					answer = new Answer(mc, mc.getAnswer());
					correct++;
				}
				else{
					//wrong
					answer = new Answer(mc, "wrong answer");
				}
				
				answers.add(answer);
				
			}
			
		}
		
		examresponse = new ExamResponse(new RealExam(1), null, answers);
	}

	@After
	public void tearDown() throws Exception {
		
		examresponse = null;
		correct = 0;
		answers = null;
	}

	@Test
	public void testComputeScore() {
		assertEquals(correct,examresponse.computeScore());
	}

	@Test
	public void testAddAnswer() {
		
		for(Answer answer: answers){
			assertEquals(false, examresponse.addAnswer(answer.getQuestion(),answer.getAnswer()));
		}
		
		for(int i=0;i<100;i++){
				
			Answer answer = null;
			
			if(i%2==0){

				TrueOrFalse tf = new TrueOrFalse("Test TF for Add"+(i), "True");
				
				answer = new Answer(tf, "False");
				
			}
			else{
				
				MultipleChoice mc = new MultipleChoice("Test MC for Add"+(i), choices.get(0), choices);
				
				answer = new Answer(mc, choices.get(1));
				
			}
		
			assertEquals(true, examresponse.addAnswer(answer.getQuestion(), answer.getAnswer()));
			
		}
		
		
		
	}

}
